package me.vudu.Conflict.Listeners.Skills;


import java.util.Random;

import me.vudu.Conflict.ConflictPlugin;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.inventory.SpoutItemStack;
import org.getspout.spoutapi.player.SpoutPlayer;

/**
 * behandelt Skills
 * FoodSeed
 * FoodHarvest
 * 
 * @author vwendel
 *
 */
public class PlayerSkillBuildListener implements Listener {

	ConflictPlugin plugin;
	Random generator;

	public PlayerSkillBuildListener(ConflictPlugin plugin) {
		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
		this.plugin = plugin;
		generator = new Random();
	}

	/**
	 * Block platzieren
	 * @param event
	 */
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerPlaceEvent(BlockPlaceEvent event) {
		SpoutPlayer p = (SpoutPlayer) event.getPlayer();
		int id = event.getBlock().getTypeId();
		
		int level = ConflictPlugin.skillBuildBuild.get(p.getName());
		if(level>0){
			if(id==1 || id == 4 || id == 98){
				int i = generator.nextInt(10);
				i+= level * 2;
				if(i>9){
					if(p.getItemInHand().getTypeId()==1 || p.getItemInHand().getTypeId()==4 || p.getItemInHand().getTypeId()==98){
						p.getItemInHand().setAmount(p.getItemInHand().getAmount()+1);
					}
				}
			}
		}
	}
	
	
	/**
	 * Stone bzw Vudubrick in VuduBrick bzw enh.VuduBrick umwandeln
	 * @param event
	 */
	@EventHandler(priority = EventPriority.HIGH)
	public void OnBlockClicked(PlayerInteractEvent event) {
		
		
		
		Block cb = event.getClickedBlock();
		SpoutPlayer pl = (SpoutPlayer)event.getPlayer();
		SpoutItemStack item = new SpoutItemStack(event.getPlayer().getItemInHand());

//		if(event.getAction() == Action.RIGHT_CLICK_BLOCK && item.getTypeId() == 318 &&
//				(item.getDurability() ==ConflictPlugin.VUDUBLOCKDUR ||  item.getDurability() ==ConflictPlugin.ENHVUDUBLOCKDUR)){
//			event.setCancelled(true);
//			return;
//		}
		
		
		if(event.getAction() == Action.LEFT_CLICK_BLOCK) {
		
			if(ConflictPlugin.skillBuildFortify.get(pl.getName()) >= 1   //geskillt
					&& item.getTypeId() == 318 && ConflictPlugin.isVuduBrick(item.getDurability())//VuduBrick in der Hand
					&& (cb.getTypeId()== 1 || cb.getTypeId()== 4 || cb.getTypeId()==98)) //Stone, CobbleTone, oder StoneBrick
					
			{
			
				SpoutManager.getMaterialManager().overrideBlock(cb, ConflictPlugin.vuduBrick);	
				int i = generator.nextInt(10);
				int level = ConflictPlugin.skillBuildVB.get(pl.getName());
				i+=level*2;
				if(i<9){//10% + 20% pro level
					int amount = pl.getItemInHand().getAmount();
					if(amount > 1){
						pl.getItemInHand().setAmount(pl.getItemInHand().getAmount()-1);
					}else{
						pl.getInventory().removeItem(pl.getItemInHand());
					}
				}
			}
			
			if(ConflictPlugin.skillBuildFortify.get(pl.getName()) == 2   //geskillt
					&& item.getTypeId() == 318 && ConflictPlugin.isEnhancedVuduBrick(item.getDurability())//EnhancedVuduBrick in der Hand
					&& cb.getTypeId()== ConflictPlugin.VUDUBRICKID) //VuduBrick
			{
				
				SpoutManager.getMaterialManager().overrideBlock(cb, ConflictPlugin.enhancedVuduBrick);		
				int i = generator.nextInt(10);
				int level = ConflictPlugin.skillBuildVB.get(pl.getName());
				i+=level*2;
				if(i<9){//10% + 20% pro level
					int amount = pl.getItemInHand().getAmount();
					if(amount > 1){
						pl.getItemInHand().setAmount(pl.getItemInHand().getAmount()-1);
					}else{
						pl.getInventory().removeItem(pl.getItemInHand());
					}
				}
			}
		}
	}
	
	/**
	 * VuduBrick platzieren
	 * @param event
	 */
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerPlaceVBEvent(BlockPlaceEvent event) {
		
	
		SpoutPlayer p = (SpoutPlayer) event.getPlayer();
		int id = event.getBlock().getTypeId();
	
		
		int level = ConflictPlugin.skillBuildVB.get(p.getName());
		
		if(level>0){
			if(id==48){
				int i = generator.nextInt(10);
			
				i+= level * 2;
				if(i>9){
					p.getItemInHand().setAmount(p.getItemInHand().getAmount()+1);
				}
			}
		}
	}
	
	
}